import { View } from "@tarojs/components";
import RestaurantItem from "./components/RestaurantItem";
import { useState } from "react";
import { useCallContainerGet } from "@/tools/useCallContainer";

import Taro from "@tarojs/taro";  
import TabBar from '@/components/TabBar';
import SearchBar from "@/components/SearchBar";

export default function index() {
  const [{ restaurants }] = useCallContainerGet("/restaurants");
  const [currentTab, setCurrentTab] = useState<number>(0);

  const handleTabClick = (index: number) => {
    setCurrentTab(index);
    if (index === 0) {
      //Taro.navigateTo({ url: './index' });
    } 
    else if (index === 1) {
      Taro.redirectTo({url:"../GetMapp/index"});
    }
    else if (index === 2) {
      Taro.redirectTo({ url: "../collections/index" });
      //Taro.navigateTo({ url: "../collections/index" });
    }
    else if (index === 3) {
      Taro.redirectTo({ url: "../Users/index" });
      //Taro.navigateTo({ url: "../collections/index" });
    }
    // 添加其他导航项的处理逻辑
  };

  const searchRestaurants = (sst:string) => {
    Taro.navigateTo({url:"../searchRestaurants/index?sst="+sst});
  }

  const searchDishes = (sst:string) => {
    Taro.navigateTo({url:"../searchDishes/index?sst="+sst});
  }

  const tabList = [
    { title: '食堂', iconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/tabbar/home1.png', selectedIconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/tabbar/home2.png' },
    { title: '地图', iconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/tabbar/map_marker_icon.svg' , selectedIconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/tabbar/map_marker_icon.svg'},
    { title: '收藏', iconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/tabbar/collection1.png', selectedIconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/tabbar/collection2.png'},
    
    { title: '用户', iconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/user_circle_light_icon.png', selectedIconPath: 'cloud://prod-0g6virbea3062996.7072-prod-0g6virbea3062996-1322280255/8726547_user_circle_icon.png'},
    // 添加其他的导航项
  ];
  
  return (
    <View style={{ padding: "10px" }}>
      <SearchBar onClick1={searchRestaurants} onClick2={searchDishes} defaultText={''} defaultOption={0} />
      {(restaurants ?? []).map((restaurant) => ( 
        <RestaurantItem restaurant={restaurant} />
      ))}
      <View className="container"> 
      <TabBar tabList={tabList} current={currentTab} onClick={handleTabClick} />
      </View>
    </View>
  );
}